@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@inject NavigationManager NavigationManager

@if (!string.IsNullOrEmpty(_message))
{
    <div class="@_classname alert-dismissible fade show mb-3" role="alert">
        @((MarkupString)_message)
        @if (Type == MessageType.Error && PageState != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
        {
            @((MarkupString)"&nbsp;&nbsp;")<NavLink href="@NavigateUrl("admin/log")">View Details</NavLink>
        }
        <button type="button" class="btn-close" aria-label="Close" @onclick="DismissModal"></button>
    </div>
}

@code {
    private string _message = string.Empty;
    private string _classname = string.Empty;

    [Parameter]
    public string Message { get; set; }

    [Parameter]
    public MessageType Type { get; set; }

    protected override void OnParametersSet()
    {
        _message = Message;
        if (!string.IsNullOrEmpty(_message))
        {
            _classname = GetMessageType(Type);
        }
    }

    private string GetMessageType(MessageType type)
    {
        string classname = string.Empty;
        switch (type)
        {
            case MessageType.Success:
                classname = "alert alert-success";
                break;
            case MessageType.Info:
                classname = "alert alert-info";
                break;
            case MessageType.Warning:
                classname = "alert alert-warning";
                break;
            case MessageType.Error:
                classname = "alert alert-danger";
                break;
        }

        return classname;
    }

    private void DismissModal()
    {
        _message = "";
        StateHasChanged();
    }
}
